# -*-coding: utf-8 -*-
"""
作者：TianyiFan
日期：2023-年11-月02日
用途：存放完全独立的通用函数模块--可以不依赖于任何程序执行的通用函数
"""

import pandas as pd
import time
import copy
import numpy as np

def sequence_strategy_1(ori_data):
    temp_data = copy.deepcopy(ori_data)
    temp_data.sort_values(by=['num_container'], ascending=[False], inplace=True)
    decision_seqence = list(temp_data['vessel_idx'])
    return decision_seqence, temp_data

def sequence_strategy_2(ori_data):
    temp_data = copy.deepcopy(ori_data)
    temp_data.sort_values(by=['seq_value', 'num_container'], ascending=[False, False], inplace=True)
    decision_seqence = list(temp_data['vessel_idx'])
    return decision_seqence, temp_data

def sort_fun(dict_obj, up_to_down=True):
    '''
    输入一个key为排序索引，value为排序权重的dict，默认降序排序，返回排序后的一个列表list
    :param dict_obj: 排序入参，dict
    :param up_to_down:排序要求，bool
    :return: list
    '''
    return sorted(dict_obj.keys(), key=lambda x: dict_obj[x], reverse=up_to_down)

def gamma_var_to_list(ganma_k):
    '''
    专门针对传入的ganma_k是模型中的变量
    :return:
    '''
    subblock = []
    n = len(ganma_k)
    for k in range(n):
        if ganma_k[k].x > 0.5:
            subblock.append(k)
    return subblock

def ganma_num_to_list(ganma_k):
    subblock = []
    n = len(ganma_k)
    for k in range(n):
        if ganma_k[k] > 0.5:
            subblock.append(k)
    return subblock

def print_swo_solution(solution_rd):
    '''
    基于存放全局结果的类，输出对应的求解结果
    :param solution_rd:
    :return:
    '''
    V = len(solution_rd.gamma_ik)
    K = len(solution_rd.gamma_ik[0])
    for i in range(1,V-1):
        print("vessel-{:3} 预留堆场:".format(i),end=" ")
        for k in range(K):
            if solution_rd.gamma_ik[i,k]>0.5:
                print(f"{k} ", end="")
        print()
    print("总成本 = {:.2f}".format(solution_rd.obj))

def remain_two(x):
    return round(x,2)

def print_pd(data):
    '''
    对齐打印输入的dataframe
    :param data:
    :return:
    '''
    pd.set_option('display.max_columns', None)
    pd.set_option('max_colwidth', 200)
    pd.set_option('display.unicode.ambiguous_as_wide', True)
    pd.set_option('display.unicode.east_asian_width', True)
    pd.set_option('display.width', 5000)  # 设置打印宽度(**重要**)
    pd.set_option('display.colheader_justify', 'center')
    print(data)

class Subblock_class:
    def __init__(self, index, blockbelong, column, row, passing, distance_berth, distance_subblock):
        self.index = index
        self.blockbelong = blockbelong
        self.columnbelong = column
        self.rowbelong = row
        self.passinglane = passing
        self.distance_berth = distance_berth  # loading 过程的距离计算
        self.unloading_distance = distance_subblock  # unloading过程的距离计算
        self.pair_belong = []  # 与当前k有关的pair

class Vessel_info:
    def __int__(self, index, loading, unloading, berth_index, r_i1):
        self.index = index
        self.loading = loading
        self.unloading = unloading
